# OddEvenEncryptionBOTH.py # # Description: Write a program that encrypts and decrypts messages # using a transposition algorithm called "odd&even". # # Anne Lavergne # Date: Feb. 5 2024 # Ask the user for a message to encrypt/decrypt plainMsg = input("Please, enter a message to encrypt/decrypt: ") # ***Encryption*** # Create two empty strings strOfOddChars = "" strOfEvenChars = "" # Start with position (i.e., index) 0 index = 0 # Consider each character in the user's plain message for char in plainMsg: # If index of character is odd if index % 2 == 1: # Put this character in strOfOddChars strOfOddChars = strOfOddChars + char # strOfOddChars =+ char else: # Otherwise put it in strOfEvenChars strOfEvenChars = strOfEvenChars + char # strOfEvenChars =+ char # Go to the next position (i.e., index) in user's plain message index = index + 1 # index += 1 # When finish, create the cipher cipherMsg = strOfOddChars + strOfEvenChars # Print the cipherMsg, i.e., the encrypted user message print(f'''\nThe original plain message you entered was "{plainMsg}". Once encrypted, your message looked like this "{cipherMsg}".''') # ***Decryption*** # Find the middle of cipherMsg middleOfCipher = len(cipherMsg) // 2 # Store the first half of cipherMsg into a string oddChars = cipherMsg[:middleOfCipher] # Store the last half of cipherMsg into a string evenChars = cipherMsg[middleOfCipher:] # Set backToPlainMsg to an empty string backToPlainMsg = "" # Then merge these two strings to recreate plainMsg # Consider each index from 0 to index at the middle of cipherMsg for index in range(middleOfCipher): # 0,1,2,3,4,5 # Concatenate the character at this index in evenChars with backToPlainMsg backToPlainMsg += evenChars[index] # Concatenate the character at this index in oddChars with backToPlainMsg backToPlainMsg += oddChars[index] # if one half is longer than the other ... if len(evenChars) > len(oddChars): # concatenate this last character to backToPlainMsg backToPlainMsg += evenChars[-1] # Print the backToPlainMsg, i.e., the decrypted message print(f'''\nThe original plain message you entered was "{plainMsg}". Once encrypted, your message looked like this "{cipherMsg}". Once this encrypted message was decrypted, we got "{backToPlainMsg}"''') if plainMsg == backToPlainMsg: print(f'And it is exactly what you originally entered! Yay!') else: print(f'And it is not quite what you originally entered! Oops!')